package cn.nuist.service.impl;

import cn.nuist.dao.ProductDao;
import cn.nuist.dao.SupplierDao;
import cn.nuist.entity.Products;
import cn.nuist.service.ProductService;
import cn.nuist.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    private ProductDao productDao;
    private SupplierDao supplierDao;


    @Autowired
    public ProductServiceImpl(ProductDao productDao, SupplierDao supplierDao) {
        this.productDao = productDao;
        this.supplierDao = supplierDao;
    }

    @Override
    public Page<Products> getViewPage(Integer pageNum) {
        Integer count = productDao.getCount();
        Page<Products> page = new Page<>();
        List<Products> list = productDao.getByPage((pageNum - 1) * page.getPageSize(),page.getPageSize());
        page.setList(list);
        page.setTotalCount(count);
        page.setCurrentPage(pageNum);
        return page;
    }

    @Override
    public Products getProductByName(String productName) {
        return productDao.getProductByName(productName);
    }

    @Override
    public Products getProductLike(String productName) {
        return productDao.getProductLike(productName);
    }

    @Override
    public void add(Products products) {
        Integer Supplierid = productDao.getSupplierIdByName(products.getSupplier());
        products.setSupplier(Supplierid+"");
        productDao.add(products);
    }

    @Override
    public void delete(Integer productID) {
        productDao.delete(productID);
    }

    @Override
    public void update(Products product) {
        Integer supplierId = productDao.getSupplierIdByName(product.getSupplier());
        product.setSupplier(supplierId+"");
        productDao.update(product);
    }

    @Override
    public Products getProductByID(Integer productID) {
        return productDao.getProductByID(productID);
    }
}
